package ieco.daoimpl;

import ieco.domaine.Inscription;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@SuppressWarnings("serial")
@Stateless
@LocalBean
public class InscriptionDaoImpl extends DaoImpl<String, Inscription> {

	@PersistenceContext(unitName="options")
	private EntityManager entityManager;
	
	public InscriptionDaoImpl() {
		super(Inscription.class);
	}

	public Inscription findInscription(int annee, int etudiant_id, int optionnel_id) {
		String query = "select i from Inscription i where i.annee = ?1 and i.etudiant.matricule = ?2 and i.optionnel.id = ?3";
		return find(query, annee, etudiant_id, optionnel_id);
	}

	public int deleteInscription(int annee, int etudiant_id, int optionnel_id){
		Query query = entityManager.createQuery("Delete from Inscription i where i.annee = ?1 and i.etudiant.matricule = ?2 and i.optionnel.id = ?3");
		query.setParameter(1, annee);
		query.setParameter(2, etudiant_id);
		query.setParameter(3, optionnel_id);
		return query.executeUpdate();
	}
	
	public List<Inscription> findAllInscriptionsPourEtudiant(int matricule) {
		String query = "select i from Inscription i where i.etudiant.matricule = ?1";
		return findAll(query, matricule);
	}
	
	public List<Inscription> findAllInscriptionsPourOptionnel(int idOptionnel){
		String query = "select i from Inscription i where i.optionnel.id = ?1";
		return findAll(query, idOptionnel);
	}
}
